﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyThietBi
{
    public partial class frmquanlyloaiphong : Form
    {
        SqlConnection con;
        CXuly_loaiphong xulyloaiphong;
        CXuly_loaivattu xulyloaivattu;
        public frmquanlyloaiphong(SqlConnection con)
        {
            InitializeComponent();
            this.con = con;
            xulyloaiphong = new CXuly_loaiphong(con);
            xulyloaivattu = new CXuly_loaivattu(con);
        }
        private void frmquanlyloaiphong_Load(object sender, EventArgs e)
        {
            hienthidsloaiphong();
        }
       
        private void hienthidsloaiphong()
        {
            lstloaiphong.Items.Clear();
            List<CLoaiphong> l = xulyloaiphong.getdsloaiphong();
            foreach (CLoaiphong x in l)
            {
                ListViewItem lvi = lstloaiphong.Items.Add(x.Maloaiphong);
                lvi.SubItems.Add(x.Tenloaiphong);
            }

        }
       
        public void hienthidsloaivattu(List<CLoaivattu> list, int rowindex)
        {
            DataGridViewComboBoxCell c = (DataGridViewComboBoxCell)dgv.Rows[rowindex].Cells[0];
            c.DataSource = list;
            c.DisplayMember = "maloai";
            c.ValueMember = "maloai";
        }
        private void dgv_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            switch (e.ColumnIndex)
            {
                case 0:
                    List<CLoaivattu> ds = xulyloaivattu.getdsloaivattu();
                    hienthidsloaivattu(ds, e.RowIndex);
                    break;


            }
        }
        public void hienthiloaivattu(CLoaivattu a, int rowindex)
        {
            dgv.Rows[rowindex].Cells[1].Value = a.Tenloai;
        }
        private void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            switch (e.ColumnIndex)
            {
                case 0:
                    DataGridViewComboBoxCell c = (DataGridViewComboBoxCell)dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    string maloaivattu = c.Value.ToString();
                    CLoaivattu x = xulyloaivattu.tim(maloaivattu);
                    if (x != null)
                    {
                        hienthiloaivattu(x, e.RowIndex);
                    }
                    break;

            }
        }
        private void hienthiloaiphong(CLoaiphong a)
        {
            txtmaloai.Text = a.Maloaiphong;
            txttenloai.Text = a.Tenloaiphong;
            dgv.Rows.Clear();
            foreach (CChitietloaiphong x in a.Chitietloaiphong)
            {
                DataGridViewRow r = dgv.Rows[dgv.Rows.Add()];
                r.Cells["maloaivattu"].Value = x.Maloaivattu;
                r.Cells["tenloaivattu"].Value = x.Loaivattu.Tenloai;
                r.Cells["soluong"].Value = x.Soluong;
            }
        }
        private void lstloaiphong_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lstloaiphong.SelectedItems)
            {
                string maloaiphong = lvi.SubItems[0].Text;
                CLoaiphong x = xulyloaiphong.tim(maloaiphong);
                foreach (CChitietloaiphong a in x.Chitietloaiphong)
                {
                    a.Loaivattu = xulyloaivattu.tim(a.Maloaivattu);
                }
                hienthiloaiphong(x);
                break;
            }
        }

        private void dgv_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            if (e.Exception.Message == "DataGridViewComboBoxCell value is not valid.")
            {
                object value = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                if (!((DataGridViewComboBoxColumn)dgv.Columns[e.ColumnIndex]).Items.Contains(value))
                {
                    ((DataGridViewComboBoxColumn)dgv.Columns[e.ColumnIndex]).Items.Add(value);
                    e.ThrowException = false;
                }
            }
        }
        
        private void btnthem_Click(object sender, EventArgs e)
        {
            CLoaiphong a = new CLoaiphong();
            a.Maloaiphong = txtmaloai.Text;
            a.Tenloaiphong = txttenloai.Text;
            List<CChitietloaiphong> list = new List<CChitietloaiphong>();
            foreach (DataGridViewRow r in dgv.Rows)
            {
                if (r.Cells["maloaivattu"].Value == null) break;
                CChitietloaiphong ct = new CChitietloaiphong();
                ct.Maloaiphong = txtmaloai.Text;
                ct.Loaivattu.Maloai = r.Cells["maloaivattu"].Value.ToString();
                ct.Loaivattu = xulyloaivattu.tim(ct.Maloaivattu);
                ct.Soluong = int.Parse(r.Cells["soluong"].Value.ToString());
                list.Add(ct);
            }
            a.Chitietloaiphong = list;
            if (xulyloaiphong.them(a) == false) MessageBox.Show("khong them duoc");
            hienthidsloaiphong();
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            string maloaiphong = txtmaloai.Text;
            xulyloaiphong.xoa(maloaiphong);
            hienthidsloaiphong();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            CLoaiphong a = new CLoaiphong();
            a.Maloaiphong = txtmaloai.Text;
            a.Tenloaiphong = txttenloai.Text;
            List<CChitietloaiphong> list = new List<CChitietloaiphong>();
            foreach (DataGridViewRow r in dgv.Rows)
            {
                if (r.Cells["maloaivattu"].Value == null) break;
                CChitietloaiphong ct = new CChitietloaiphong();
                ct.Maloaiphong = txtmaloai.Text;
                ct.Loaivattu.Maloai = r.Cells["maloaivattu"].Value.ToString();
                ct.Loaivattu = xulyloaivattu.tim(ct.Maloaivattu);
                ct.Soluong = int.Parse(r.Cells["soluong"].Value.ToString());
                list.Add(ct);
            }
            a.Chitietloaiphong = list;
            xulyloaiphong.sua(a);
            hienthidsloaiphong();
        }
    }
}
